<template>
  <div class="line"><div class="echart" ref="echart"></div></div>
</template>

<script>
export default {
  props: ['config'],
  data() {
    return {
      chartInit: null
    };
  },
  created() {},
  mounted() {
    this.initEcharts();
    window.addEventListener('resize', this.screenAdapter);
  },
  destroyed() {
    window.removeEventListener('resize', this.screenAdapter);
  },
  methods: {
    initEcharts() {
      this.chartInit = this.$echarts.init(this.$refs.echart, 'biluo');
      const option = {
        title: {
          text: '电子班牌使用情况',
          left: 'center'
        },
        tooltip: {
          trigger: 'item'
        },
        legend: {
          //图例垂直排列
          orient: 'vertical',
          x: 'right',
          y: 'bottom',
          //data中的名字要与series-data中的列名对应，方可点击操控
          data: ['使用', '未使用']
        },
        series: [
          {
            name: '使用率',
            type: 'pie',
            radius: '70%',
            center: ['50%', '60%'],
            label: {
              normal: {
                show: true,
                position: 'inside',
                formatter: '{d}%', //模板变量有 {a}、{b}、{c}、{d}，分别表示系列名，数据名，数据值，百分比。{d}数据会根据value值计算百分比
                textStyle: {
                  align: 'center',
                  baseline: 'middle',
                  fontFamily: '微软雅黑',
                  fontSize: 13,
                  color: '#ffffff',
                  fontWeight: 'bolder'
                }
              }
            },
            data: [{ value: 2048, name: '使用' }, { value: 300, name: '未使用' }],
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      };
      this.chartInit.setOption(option);
    },
    screenAdapter() {
      //和分辨率先关的配置
      const titleFontSize = (this.$refs.echart.offsetWidth / 100) * 3.6;
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize
          }
        },
        tooltip: {
          axisPointer: {
            lineStyle: {
              width: titleFontSize
            }
          }
        },
        series: [
          {
            // barWidth: titleFontSize, //圆柱的宽度
          }
        ]
      };
      this.chartInit.setOption(adapterOption);
      //手动调用改变图标大小
      this.chartInit.resize();
    }
  }
};
</script>

<style lang="less" scoped>
.line {
  height: 100%;
  .echart {
    height: 100%;
  }
}
</style>
